{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4ef4e564-e056-4658-867c-f8ff7cead982",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # linear algebra\n",
    "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n",
    "# import seaborn as sns \n",
    "import matplotlib.pyplot as plt\n",
    "# machine learning\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.svm import SVC, LinearSVC\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "from sklearn.linear_model import Perceptron\n",
    "from sklearn.linear_model import SGDClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "# import seaborn as sns \n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import cross_val_score\n",
    "from sklearn.model_selection import KFold\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn import preprocessing\n",
    "from numpy import array\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import precision_score, recall_score, f1_score,accuracy_score, roc_auc_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2b102c1a-3b32-4c63-b374-bd838c02a85d",
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.read_csv('train.csv')\n",
    "test = pd.read_csv('test.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fdbf63c8-0cac-4e7a-a9f7-e9cfe0e81fa3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Sunshine', 'Evaporation', 'Cloud3pm', 'Cloud9am']\n"
     ]
    }
   ],
   "source": [
    "# 找出缺失数据多的列\n",
    "zeros_cnt = train.isnull().sum().sort_values(ascending=False)\n",
    "percent_zeros = (train.isnull().sum() / train.isnull().count()).sort_values(ascending=False)\n",
    "\n",
    "missing_data = pd.concat([zeros_cnt, percent_zeros], axis=1, keys=['Total', 'Percent'])\n",
    "dropList = list(missing_data[missing_data['Percent'] > 0.15].index)\n",
    "print(dropList)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9e33888e-01a8-436e-a1dc-351cec1b0b7a",
   "metadata": {},
   "outputs": [],
   "source": [
    "train.drop(['Sunshine', 'Evaporation', 'Cloud3pm', 'Cloud9am'], axis=1, inplace=True)\n",
    "test.drop(['Sunshine', 'Evaporation', 'Cloud3pm', 'Cloud9am'], axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "dba659e2-80c2-48f4-a620-0da9b2333acd",
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.get_dummies(data=train, columns=['WindGustDir','WindDir9am','WindDir3pm'])\n",
    "test = pd.get_dummies(data=test, columns=['WindGustDir','WindDir9am','WindDir3pm'])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "98b57232-e1fb-439a-9644-1e18bd36407b",
   "metadata": {},
   "outputs": [],
   "source": [
    "lb = preprocessing.LabelBinarizer()\n",
    "\n",
    "train['RainToday'] = train['RainToday'].astype(str)\n",
    "train['RainTomorrow'] = train['RainTomorrow'].astype(str)\n",
    "lb.fit(train['RainToday'])\n",
    "train['RainToday'] = lb.transform(train['RainToday'])\n",
    "train['RainTomorrow'] = lb.transform(train['RainTomorrow'])\n",
    "\n",
    "test['RainToday'] = test['RainToday'].astype(str)\n",
    "test['RainToday'] = lb.transform(test['RainToday'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e7542363-8b1c-459e-b463-d905f70c7d5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "train.drop(['Date', 'Location'], axis=1, inplace=True)\n",
    "train = train.fillna(train.mean())\n",
    "test = test.fillna(test.mean())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "e6d3c95b-e554-4866-baa3-bb256d2b501d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(81457, 61) (34911, 61) (81457,) (34911,)\n"
     ]
    }
   ],
   "source": [
    "# 训练集上\n",
    "y = train['RainTomorrow']\n",
    "X = train.drop(['RainTomorrow'], axis=1)\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)\n",
    "print(X_train.shape, X_test.shape, y_train.shape, y_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b8299ef-b1b4-4d91-93ac-ad60643a703a",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train['RainToday']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "63b98238-b88d-490f-a80f-e70f51d999cc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9998895122580012"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pipe = Pipeline([('scaler', StandardScaler()), ('RFC', RandomForestClassifier(\n",
    "#     criterion='gini', \n",
    "#                                                                               max_depth=10, \n",
    "#                                                                               max_features='auto',\n",
    "#                                                                               class_weight='balanced',\n",
    "#                                                                               n_estimators=200\n",
    "    n_jobs=-1\n",
    "                                                                             ))])\n",
    "pipe.fit(X_train, y_train)\n",
    "y_pred = pipe.predict(X_test)\n",
    "pipe.score(X_train, y_train)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e62d1dcd-f9b9-45fc-ba99-2b405b1261ec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acc: 0.8437741686001546\n",
      "f1: 0.9023070860500108\n",
      "recall: 0.9513503305004721\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Location</th>\n",
       "      <th>MinTemp</th>\n",
       "      <th>MaxTemp</th>\n",
       "      <th>Rainfall</th>\n",
       "      <th>WindGustSpeed</th>\n",
       "      <th>WindSpeed9am</th>\n",
       "      <th>WindSpeed3pm</th>\n",
       "      <th>Humidity9am</th>\n",
       "      <th>Humidity3pm</th>\n",
       "      <th>...</th>\n",
       "      <th>WindDir3pm_NNW</th>\n",
       "      <th>WindDir3pm_NW</th>\n",
       "      <th>WindDir3pm_S</th>\n",
       "      <th>WindDir3pm_SE</th>\n",
       "      <th>WindDir3pm_SSE</th>\n",
       "      <th>WindDir3pm_SSW</th>\n",
       "      <th>WindDir3pm_SW</th>\n",
       "      <th>WindDir3pm_W</th>\n",
       "      <th>WindDir3pm_WNW</th>\n",
       "      <th>WindDir3pm_WSW</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2009/12/10</td>\n",
       "      <td>MountGinini</td>\n",
       "      <td>6.2</td>\n",
       "      <td>16.2</td>\n",
       "      <td>0.2</td>\n",
       "      <td>61.0</td>\n",
       "      <td>30.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>49.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2017/3/11</td>\n",
       "      <td>Nhil</td>\n",
       "      <td>14.4</td>\n",
       "      <td>29.3</td>\n",
       "      <td>0.0</td>\n",
       "      <td>44.0</td>\n",
       "      <td>19.0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>54.0</td>\n",
       "      <td>29.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2014/12/2</td>\n",
       "      <td>Uluru</td>\n",
       "      <td>24.4</td>\n",
       "      <td>41.8</td>\n",
       "      <td>0.0</td>\n",
       "      <td>52.0</td>\n",
       "      <td>15.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>33.0</td>\n",
       "      <td>17.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2017/2/8</td>\n",
       "      <td>PerthAirport</td>\n",
       "      <td>14.6</td>\n",
       "      <td>26.7</td>\n",
       "      <td>0.0</td>\n",
       "      <td>59.0</td>\n",
       "      <td>31.0</td>\n",
       "      <td>35.0</td>\n",
       "      <td>41.0</td>\n",
       "      <td>23.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2013/1/2</td>\n",
       "      <td>Portland</td>\n",
       "      <td>8.3</td>\n",
       "      <td>24.0</td>\n",
       "      <td>0.0</td>\n",
       "      <td>26.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>11.0</td>\n",
       "      <td>62.0</td>\n",
       "      <td>50.0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 63 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "         Date      Location  MinTemp  MaxTemp  Rainfall  WindGustSpeed  \\\n",
       "0  2009/12/10   MountGinini      6.2     16.2       0.2           61.0   \n",
       "1   2017/3/11          Nhil     14.4     29.3       0.0           44.0   \n",
       "2   2014/12/2         Uluru     24.4     41.8       0.0           52.0   \n",
       "3    2017/2/8  PerthAirport     14.6     26.7       0.0           59.0   \n",
       "4    2013/1/2      Portland      8.3     24.0       0.0           26.0   \n",
       "\n",
       "   WindSpeed9am  WindSpeed3pm  Humidity9am  Humidity3pm  ...  WindDir3pm_NNW  \\\n",
       "0          30.0          20.0         52.0         49.0  ...               0   \n",
       "1          19.0          20.0         54.0         29.0  ...               0   \n",
       "2          15.0          26.0         33.0         17.0  ...               0   \n",
       "3          31.0          35.0         41.0         23.0  ...               0   \n",
       "4          11.0          11.0         62.0         50.0  ...               0   \n",
       "\n",
       "   WindDir3pm_NW  WindDir3pm_S  WindDir3pm_SE  WindDir3pm_SSE  WindDir3pm_SSW  \\\n",
       "0              0             0              0               0               0   \n",
       "1              0             1              0               0               0   \n",
       "2              1             0              0               0               0   \n",
       "3              0             0              0               1               0   \n",
       "4              0             0              0               0               0   \n",
       "\n",
       "   WindDir3pm_SW  WindDir3pm_W  WindDir3pm_WNW  WindDir3pm_WSW  \n",
       "0              0             0               1               0  \n",
       "1              0             0               0               0  \n",
       "2              0             0               0               0  \n",
       "3              0             0               0               0  \n",
       "4              0             0               0               0  \n",
       "\n",
       "[5 rows x 63 columns]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(\"acc: {}\".format(accuracy_score(y_pred, y_test)))\n",
    "print(\"f1: {}\".format(f1_score(y_test, y_pred)))\n",
    "print(\"recall: {}\".format(recall_score(y_test, y_pred)))\n",
    "test.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "88e48577-3be3-4dbb-81b8-f18eb8c731bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(29092, 61)\n"
     ]
    }
   ],
   "source": [
    "# start predicting\n",
    "X_train = train.drop(['RainTomorrow'], axis=1)\n",
    "y_train = train['RainTomorrow']\n",
    "X_test = test.drop(['Date', 'Location'], axis=1)\n",
    "print(X_test.shape)\n",
    "pipe.fit(X_train, y_train)\n",
    "y_pred = pipe.predict(X_test)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "aac083c4-1766-42d3-b173-8e24402b4b1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(test[['Date','Location']])\n",
    "df['RainTomorrow'] = y_pred\n",
    "df['RainTomorrow'] = df['RainTomorrow'].apply(lambda x : 'No' if x == 1 else 'Yes')\n",
    "df.to_csv('out.csv', index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
